home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GDEVPCCM.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  4KB  |  133 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevpccm.c */
  20. /* Support routines for PC color mapping */
  21. #include "gs.h"
  22. #include "gsmatrix.h"            /* for gxdevice.h */
  23. #include "gxdevice.h"
  24. #include "gdevpccm.h"            /* interface */
  25.  
  26. /* Color mapping routines for EGA/VGA-style color. */
  27. /* Colors are 4 bits: 8=intensity, 4=R, 2=G, 1=B. */
  28.  
  29. #define black 0
  30. #define blue 1
  31. #define green 2
  32. #define cyan 3
  33. #define red 4
  34. #define magenta 5
  35. #define brown 6
  36. #define white 7
  37. #define dgray 8                /* dark gray is not very usable */
  38. #define lblue 9
  39. #define lgreen 10
  40. #define lcyan 11
  41. #define lred 12
  42. #define lmagenta 13
  43. #define yellow 14
  44. #define bwhite 15
  45. gx_color_index
  46. pc_4bit_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  47.   gx_color_value b)
  48. {
  49. #define c13 (gx_max_color_value / 3)
  50. #define c23 (gx_max_color_value - c13)
  51.     static byte g0[3][3] =
  52.      {{black,blue,lblue},{red,magenta,lmagenta},{lred,lmagenta,lmagenta}};
  53.     static byte g1[3][3] =
  54.      {{green,cyan,lcyan},{brown,white,lcyan},{yellow,yellow,lmagenta}};
  55.     static byte g2[3][3] =
  56.      {{lgreen,lgreen,lcyan},{lgreen,lgreen,lcyan},{yellow,yellow,bwhite}};
  57.      int ri = (r >= c23 ? 2 : r >= c13 ? 1 : 0);
  58.     int bi = (b >= c23 ? 2 : b >= c13 ? 1 : 0);
  59.     return (gx_color_index)
  60.         (g >= c23 ? g2[ri][bi] : g >= c13 ? g1[ri][bi] : g0[ri][bi]);
  61. #undef c13
  62. #undef c23
  63. }
  64. int
  65. pc_4bit_map_color_rgb(gx_device *dev, gx_color_index color,
  66.   gx_color_value prgb[3])
  67. {
  68. #define icolor (int)color
  69.     gx_color_value one =
  70.         (icolor & 8 ? gx_max_color_value : gx_max_color_value / 3);
  71.     prgb[0] = (icolor & 4 ? one : 0);
  72.     prgb[1] = (icolor & 2 ? one : 0);
  73.     prgb[2] = (icolor & 1 ? one : 0);
  74.     return 0;
  75. #undef icolor
  76. }
  77.  
  78. /* Color mapping routines for 8-bit color with a fixed palette */
  79. /* (3 bits of R, 3 bits of G, 2 bits of B). */
  80. /* We have to trade off even spacing of colors along each axis */
  81. /* against the desire to have real gray shades; */
  82. /* we compromise by using a 7x7x4 "cube" with extra gray shades */
  83. /* (1/6, 1/2, and 5/6), instead of the obvious 8x8x4. */
  84.  
  85. gx_color_index
  86. pc_8bit_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  87.   gx_color_value b)
  88. {    uint rv = r / (gx_max_color_value / 7 + 1);
  89.     uint gv = g / (gx_max_color_value / 7 + 1);
  90.     uint bv = b / (gx_max_color_value / 7 + 1);
  91.     return (gx_color_index)
  92.         (rv == gv && gv == bv ? rv + (256-7) :
  93.          (rv << 5) + (gv << 2) + (bv >> 1));
  94. }
  95. int
  96. pc_8bit_map_color_rgb(gx_device *dev, gx_color_index color,
  97.   gx_color_value prgb[3])
  98. {    static const gx_color_value ramp[8] =
  99.     {    0, gx_max_color_value / 6, gx_max_color_value / 3,
  100.         gx_max_color_value / 2, 2 * (gx_max_color_value / 3),
  101.         5 * (gx_max_color_value / 6), gx_max_color_value,
  102.         /* The 8th entry is not actually ever used, */
  103.         /* except to fill out the palette. */
  104.         gx_max_color_value
  105.     };
  106. #define icolor (uint)color
  107.     if ( icolor >= 256-7 )
  108.     {    prgb[0] = prgb[1] = prgb[2] = ramp[icolor - (256-7)];
  109.     }
  110.     else
  111.     {    prgb[0] = ramp[(icolor >> 5) & 7];
  112.         prgb[1] = ramp[(icolor >> 2) & 7];
  113.         prgb[2] = ramp[(icolor & 3) << 1];
  114.     }
  115. #undef icolor
  116.     return 0;
  117. }
  118.  
  119. /* Write a palette on a file. */
  120. int
  121. pc_write_palette(gx_device *dev, uint max_index, FILE *file)
  122. {    uint i, c;
  123.     gx_color_value rgb[3];
  124.     for ( i = 0; i < max_index; i++ )
  125.     {    (*dev->procs->map_color_rgb)(dev, (gx_color_index)i, rgb);
  126.         for ( c = 0; c < 3; c++ )
  127.         {    byte b = rgb[c] >> (gx_color_value_bits - 8);
  128.             fputc(b, file);
  129.         }
  130.     }
  131.     return 0;
  132. }
  133.